<template>
  <div class="login">
    <a-card class="login_center">
      <template #title>
        <div class="card_header">
          <span>智能警报分发系统</span>
        </div>
      </template>
      <a-form :model="loginFormState" :rules="rules">
        <a-form-item name="userName">
          <a-input
            v-model:value="loginFormState.userName"
            :maxlength="32"
            placeholder="请输入账号"
            clearable
          >
            <template #prefix>
              <UserOutlined />
            </template>
          </a-input>
        </a-form-item>
        <a-form-item name="pwd">
          <a-input
            v-model:value="loginFormState.pwd"
            :maxlength="16"
            show-password
            placeholder="请输入密码"
            clearable
            @keyup.enter.exact="handleLogin"
          >
            <template #prefix>
              <LockOutlined />
            </template>
          </a-input>
        </a-form-item>
        <a-form-item>
          <a-button
            type="primary"
            style="width: 100%"
            :loading="loading"
            @click="handleLogin"
            >登 录</a-button
          >
        </a-form-item>
      </a-form>
    </a-card>
  </div>
</template>

<script setup>
import { reactive, ref } from "vue";
import { useRouter } from "vue-router";
import { useMainStore } from "@/store/index.js";
import { Form } from "ant-design-vue";
import { UserOutlined, LockOutlined } from "@ant-design/icons-vue";
const useForm = Form.useForm;

const router = useRouter();
const mainStore = useMainStore();

const loading = ref(false);

const loginFormState = reactive({
  userName: "",
  pwd: "",
});

const rules = reactive({
  userName: [{ required: true, message: "账号不能为空", trigger: "blur" }],
  pwd: [
    { required: true, message: "密码不能为空", trigger: "blur" },
    { min: 5, max: 16, message: "密码长度为5-16位", trigger: "blur" },
  ],
});

const { validate } = useForm(loginFormState, rules);

const handleLogin = () => {
  validate().then(() => {
    loading.value = true;
    const params = {
      userName: loginFormState.userName,
      pwd: loginFormState.pwd,
    };

    mainStore.login(params).then(() => {
      loading.value = false;
      router.push("/");
    });
  });
};
</script>

<style lang="less" scoped>
.login {
  width: 100vw;
  height: 100vh;
  background-image: url("../../assets/img/login.jpg");
  background-size: cover;
  display: flex;
  justify-content: center;
  align-items: center;
  overflow: hidden;

  .login_center {
    width: 396px;
    height: auto;
  }

  .card_header {
    font-size: 18px;
    text-align: center;
  }
}
</style>
